package com.bacon.gmall.list.listener;

import com.bacon.gmall.common.config.RabbitConst;
import com.bacon.gmall.list.service.ListService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-09 23:28
 * @description: 接收MQ消息
 */
@Component

public class ESConsumer {



    @Autowired
    private ListService listService;

    //接收上架消息
    /*@RabbitListener(queues = "")*/
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = RabbitConst.ROUTING_GOODS_UPPER,
                    durable = "true",
                    autoDelete = "false"),
            exchange = @Exchange(value = RabbitConst.EXCHANGE_DIRECT_GOODS),
            key = {RabbitConst.ROUTING_GOODS_UPPER}

    )})
    public void receiverUpperMessage(Long skuId, Channel channel, Message message) {

        try {
            System.out.println("skuId ---> " + skuId);
            listService.onSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //接收下架消息
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = RabbitConst.QUEUE_GOODS_LOWER,
                    durable = "true",
                    autoDelete = "false"),
            exchange = @Exchange(value = RabbitConst.EXCHANGE_DIRECT_GOODS),
            key = {RabbitConst.ROUTING_GOODS_LOWER}

    )})
    public void receiverLowerMessage(Long skuId, Channel channel, Message message) {
        try {

            System.out.println("skuId ---> " + skuId);
            listService.cancelSale(skuId);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
